package ide.ccf.ccproject.pages;

import ide.ccf.ccproject.common.EditorInputWatcher;
import ide.ccf.ccproject.editors.ResourceEditor;
import ide.ccf.ccproject.pages.embedded.ResourceTreeView;
import ide.ccf.ccproject.pages.embedded.ResourceTreeViewListener;

import java.io.File;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.part.FileEditorInput;


public class ResourceTreePage extends FormPage implements ResourceTreeViewListener
{
	private ResourceEditor m_editor;
	private ResourceTreeView resourcetreeview;

	/**
	 * 构造器
	 * @param editor
	 */
	public ResourceTreePage(ResourceEditor editor)
	{
		super(editor, "ide.ccf.ccproject.pages.ResourceTreePage", "CCF资源");
		m_editor = editor;
	}

	/**
	 * 设置为激活
	 */
	public void setActive(boolean active)
	{
		super.setActive(active);
	}
	
	/**
	 * 选中
	 * @param e
	 */
	public void widgetSelected(SelectionEvent e)
	{

	}
	
	/**
	 * 默认选中
	 * @param e
	 */
	public void widgetDefaultSelected(SelectionEvent e)
	{
		
	}
	
	/**
	 * 数据是否过时
	 */
	public boolean isDirty()
	{
		return (resourcetreeview != null ? resourcetreeview.isDirty() : false);
	}
	
	/**
	 * 保存
	 */
	public void doSave(IProgressMonitor monitor)
	{
		if (resourcetreeview == null)
			return;
		
		FileEditorInput fin = (FileEditorInput)getEditorInput();
		
		if (!resourcetreeview.save()) 
		{
    		MessageDialog.openError(getSite().getShell(), "保存文件出错", "无法写入文件到: " + fin.getFile().getLocation().toOSString() + "目录\n确认该目录可写。");
		}
	}
	
	/**
	 * 创建编辑器窗体内容
	 */
	protected void createFormContent(IManagedForm managedForm)
	{
		Composite parent = managedForm.getForm().getBody();
		FileEditorInput fin = (FileEditorInput)getEditorInput();
		String fileName = fin.getFile().getLocation().toOSString();
		File file = new File(fileName);
		boolean readable = file.exists() && file.canRead();
		
		if (!readable) 
		{
			EditorInputWatcher.createMissingFileInfo(parent, fileName);
			return;
		} 
		
		FormToolkit toolkit = managedForm.getToolkit();
		GridLayout layout = new GridLayout(1, false);
		parent.setLayout(layout);
		
		createResourceTreeView(toolkit, parent);
		resourcetreeview.load(fileName);
	}

	/**
	 * 重新载入
	 */
	public void reload() 
	{
		FileEditorInput fin = (FileEditorInput)getEditorInput();
		String fileName = fin.getFile().getLocation().toOSString();
		resourcetreeview.load(fileName);
	}

	/**
	 * 创建资源树视图
	 * @param toolkit
	 * @param parent
	 */
	private void createResourceTreeView(FormToolkit toolkit, Composite parent)
	{
		resourcetreeview = new ResourceTreeView(parent, SWT.EMBEDDED);
		resourcetreeview.addResourceTreeViewListener(this);
		resourcetreeview.setLayoutData(new GridData(500, 400));
		toolkit.adapt(resourcetreeview);
		toolkit.paintBordersFor(resourcetreeview);
	}
	
	/**
	 * 返回内容
	 * @return
	 */
	public String contents() 
	{
		return (resourcetreeview != null ? resourcetreeview.contents() : "");
	}

	/**
	 * 编辑器数据过时状态改变
	 */
	public void dirtyChanged() 
	{
		m_editor.editorDirtyStateChanged();
	}

	/**
	 * 设置编辑器获取焦点
	 */
	public void setFocus() 
	{
		super.setFocus();
		
		if (resourcetreeview != null)
			resourcetreeview.setFocus();
	}
}